package supertuxwar.powerup;

import net.phys2d.raw.Body;
import net.phys2d.raw.CollisionEvent;
import net.phys2d.raw.shapes.Box;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import supertuxwar.AbstractEntity;
import supertuxwar.Actor;
import supertuxwar.Entity;
import supertuxwar.TileEnvironment;

/**
 * A simple crate showing a default physics body being used. This entity doesn't
 * have the "special" rules applied to it that an Actor does.
 * 
 * @author kevin
 */
public class Bullet extends AbstractEntity {

	private Powerup powerup;

	public String getName() {
		return "Bullet";
	}

	/**
	 * Create a new crate
	 * 
	 * @param x
	 *            The x position of the centre of the crate
	 * @param y
	 *            The y position fo the centre of the crate
	 * @param width
	 *            The width of the crate
	 * @param height
	 *            The height of the crate
	 * @param mass
	 *            The mass of the crate
	 * @throws SlickException
	 *             Indicates a failure to load the resources
	 */
	public Bullet(float x, float y, float mass, Powerup powerup)
			throws SlickException {
		this.powerup = powerup;

		body = new Body(new Box(10, 4), mass);
		body.setUserData(this);
		body.setPosition(x, y);
		body.setFriction(0.1f);
		body.setRotatable(false);
		body.setGravityEffected(false); // 子弹不受重力影响
	}

	/**
	 * @see org.newdawn.penguin.Entity#getBody()
	 */
	public Body getBody() {
		return body;
	}

	/**
	 * @see org.newdawn.penguin.Entity#preUpdate(int)
	 */
	public void preUpdate(int delta) {
	}

	/**
	 * @see org.newdawn.penguin.Entity#render(org.newdawn.slick.Graphics)
	 */
	public void render(Graphics g) {
		g.fillRect(getX() - 5, getY() - 2, 10, 4);
	}

	/**
	 * @see org.newdawn.penguin.Entity#update(int)
	 */
	public void update(int delta) {
		if (world == null) {
			return;
		}

		// loop through the collision events that have occured in the
		// world
		CollisionEvent[] events = world.getContacts(body);

		for (int i = 0; i < events.length; i++) {

			// TODO: 两个子弹对射
			if (events[i].getBodyA() == body) {
				Object obj = events[i].getBodyB().getUserData();
				if (obj != null) { //
					System.out.println(((Entity) obj).getName());
					if (obj instanceof Actor) {
						Actor actor = (Actor) obj; // 子弹遇到玩家
						if (actor != powerup.getActor()) {
							actor.setDeath(true);
							TileEnvironment.getInstance().removeEntity(this);
						}
					} else if (obj instanceof Bullet) { // 子弹遇到子弹，同时消亡
						TileEnvironment.getInstance().removeEntity(this); //
						TileEnvironment.getInstance()
								.removeEntity((Bullet) obj);
					} else {
						System.out.println(((Entity) obj).getName());
						TileEnvironment.getInstance().removeEntity(this);
					}
				}

			} else {
				Object obj = events[i].getBodyA().getUserData();
				if (obj != null) { 
					System.out.println(((Entity)obj).getName());
					if (obj instanceof Actor) {
						Actor actor = (Actor) obj; // 子弹遇到玩家
						if (actor != powerup.getActor()) {
							actor.setDeath(true);
							TileEnvironment.getInstance().removeEntity(this);
						}
					} else if (obj instanceof Bullet) { // 子弹遇到子弹，同时消亡
						TileEnvironment.getInstance().removeEntity(this); //
						TileEnvironment.getInstance()
								.removeEntity((Bullet) obj);
					} else {
						TileEnvironment.getInstance().removeEntity(this);
					}
				}
			}
		}
	}
}
